<template>
    <div>
        <!-- 井眼 -->
        <el-card :style="{ height: height }" class="card">
            <!-- <el-card style="height: 800px" class="card"> -->
            <div slot="header">
                <span>井眼/ 基础数据</span>
            </div>
            <el-row>
                <el-row>
                    <!-- 查询栏 -->
                    <el-form :inline="true" :model="searchModel">
                        <el-col :sm="6" :md="6" :lg="3" :xl="3">
                            <el-form-item>
                                <el-input placeholder="请填写井名" v-model="searchModel.wellName" clearable maxlength="20" />
                            </el-form-item>
                        </el-col>
                        <el-col :sm="6" :md="6" :lg="3" :xl="3">
                            <el-form-item>
                                <el-select v-model="searchModel.wellType" clearable placeholder="请选择井型">
                                    <el-option
                                        v-for="item in wellClassesOptions"
                                        :key="item"
                                        :label="item"
                                        :value="item"
                                        :disabled="item.disabled"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :sm="6" :md="6" :lg="3" :xl="3">
                            <el-form-item>
                                <el-select v-model="searchModel.wellCategory" clearable placeholder="请选择井别">
                                    <el-option
                                        v-for="item in wellTypeOptions"
                                        :key="item"
                                        :label="item"
                                        :value="item"
                                        :disabled="item.disabled"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :sm="12" :md="12" :lg="6" :xl="4">
                            <el-form-item>
                                <el-date-picker
                                    v-model="searchModel.startDrillDate"
                                    type="date"
                                    align="right"
                                    value-format="yyyy-MM-dd"
                                    style="width: 47%"
                                    placeholder="开始日期"
                                >
                                </el-date-picker>
                                <span style="color: #c0c0c0">&nbsp;-&nbsp;</span>
                                <el-date-picker
                                    v-model="searchModel.endDrillDate"
                                    type="date"
                                    align="right"
                                    value-format="yyyy-MM-dd"
                                    style="width: 47%"
                                    placeholder="结束日期"
                                >
                                </el-date-picker>
                            </el-form-item>
                        </el-col>

                        <el-button type="primary" @click="submitSearchModel">查询</el-button>

                        <el-button type="success" @click="submitAddModel">新增</el-button>

                        <!-- <el-button type="success">导入</el-button> -->
                        <!-- excel导入 -->
                        <div style="margin-left: 10px; display: inline-block; margin-right:10px">
                            <el-upload class="upload-demo" :http-request="ImportWell" :show-file-list="false" action="">
                                <el-button type="success">导入</el-button>
                            </el-upload>
                        </div>

                        <el-button type="primary" @click="DownLoadModel">模板下载</el-button>
                    </el-form>
                </el-row>
                <!-- 查询结果表格展示 -->
                <el-row style="width: 100%">
                    <el-table
                        :data="tableData"
                        border
                        style="width: 100%; font-size: 10px"
                        :max-height="tableHeight"
                        :header-row-style="rowStyle"
                        :header-cell-style="cellStyle"
                        :row-style="rowStyle"
                        :cell-style="cellStyle"
                        stripe
                    >
                        <el-table-column label="操作" width="220" fixed="left">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                                <el-button type="danger" size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                                <el-button type="success" size="mini" @click="handleImport(scope.$index, scope.row)">录入</el-button>
                            </template>
                        </el-table-column>
                        <!-- <el-table-column
                            v-if="tableHead[0]"
                            :label="tableHead[0].label"
                            :prop="tableHead[0].property"
                            width="100"
                            align="center"
                        /> -->
                        <el-table-column
                            v-for="(item, index) in tableHead.slice(1)"
                            :label="item.label"
                            :property="item.property"
                            :key="index"
                            width="95"
                            show-overflow-tooltip
                            align="center"
                        />
                    </el-table>
                    <!-- 显示页数 -->
                    <div class="pagination">
                        <el-pagination
                            background
                            layout="total, prev, pager, next"
                            :current-page="pageIdx"
                            :page-size="pageSizes"
                            :total="totalData"
                            @current-change="handlePageChange"
                        ></el-pagination>
                    </div>
                    <!-- 编辑弹出框 -->
                    <el-dialog title="基础数据修改" :visible.sync="editVisible" width="60%">
                        <el-form :model="form" ref="form" label-width="120px">
                            <!-- 自定义编辑内容样式 -->
                            <el-col :span="12">
                                <el-form-item label="井名">
                                    <el-input v-model="form.wellname"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="油田">
                                    <template>
                                        <el-select v-model="form.oilfield" clearable placeholder="请选择油田" style="width: 100%">
                                            <el-option
                                                v-for="item in oilfieldOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="区块">
                                    <template>
                                        <el-select v-model="form.block" clearable placeholder="请选择区块" style="width: 100%">
                                            <el-option
                                                v-for="item in BlockOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="项目单位">
                                    <template>
                                        <el-select v-model="form.department" clearable placeholder="请选择项目单位" style="width: 100%">
                                            <el-option
                                                v-for="item in departmentOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="钻井公司">
                                    <template>
                                        <el-select
                                            v-model="form.drillwellcompany"
                                            clearable
                                            placeholder="请选择钻井公司"
                                            style="width: 100%"
                                        >
                                            <el-option
                                                v-for="item in DrillWellCompanyOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="钻探公司">
                                    <template>
                                        <el-select v-model="form.drillcompany" clearable placeholder="请选择钻探公司" style="width: 100%">
                                            <el-option
                                                v-for="item in DrillCompanyOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="钻井队号">
                                    <el-input v-model="form.drillteam"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="油藏类型">
                                    <template>
                                        <el-select v-model="form.reservoirtype" clearable placeholder="请选择油藏类型" style="width: 100%">
                                            <el-option
                                                v-for="item in ReservoirTypeOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="搬安日期">
                                    <template>
                                        <el-date-picker
                                            v-model="form.resetdate"
                                            align="right"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期"
                                            style="width: 100%"
                                        >
                                        </el-date-picker>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="井型">
                                    <template>
                                        <el-select v-model="form.welltype" clearable placeholder="请选择井型" style="width: 100%">
                                            <el-option
                                                v-for="item in wellClassesOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="井别">
                                    <template>
                                        <el-select v-model="form.wellcategory" clearable placeholder="请选择井别" style="width: 100%">
                                            <el-option
                                                v-for="item in wellTypeOptions"
                                                :key="item"
                                                :label="item"
                                                :value="item"
                                                :disabled="item.disabled"
                                            >
                                            </el-option>
                                        </el-select>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="开钻日期">
                                    <template>
                                        <el-date-picker
                                            v-model="form.startdrilldate"
                                            align="right"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期"
                                            style="width: 100%"
                                        >
                                        </el-date-picker>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="完钻日期">
                                    <template>
                                        <el-date-picker
                                            v-model="form.enddrilldate"
                                            align="right"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期"
                                            style="width: 100%"
                                        >
                                        </el-date-picker>
                                    </template>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="设计井深(m)">
                                    <el-input v-model="form.welldepth1"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="完钻井深(m)">
                                    <el-input v-model="form.welldepth2"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="完井层位">
                                    <el-input v-model="form.finishposition"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="目的层">
                                    <el-input v-model="form.objectposition"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="井身结构钻头程序">
                                    <el-input type="textarea" v-model="form.wellstructure"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="井身结构套管程序">
                                    <el-input type="textarea" v-model="form.caseprog"></el-input>
                                </el-form-item>
                            </el-col>
                            <!-- 此处还可以添加内容 -->
                        </el-form>
                        <span slot="footer" class="dialog-footer">
                            <el-button @click="editVisible = false">取 消</el-button>
                            <el-button type="primary" @click="saveNew" v-if="isNewData">确 定</el-button>
                            <el-button type="primary" @click="saveEdit" v-else>确 定</el-button>
                        </span>
                    </el-dialog>
                </el-row>
            </el-row>
        </el-card>
    </div>
</template>

<script>
import {
    GetDataClassificationMenus,
    GetTableHead,
    GetTableData,
    ListDataDictByCondition
} from '../../../../api/module/dataClassification/data_classification';
import { EditOneWell, DeleteOneWell, FindWellByCondition, InsertOneWell } from '../../../../api/module/dataClassification/basicData/well';
import { importExcel, downTemplateExcel } from '../../../../api/module/dataClassification/excel';
export default {
    data() {
        return {
            fileList: [],
            // menuName: this.$route.query.menuName,  //页面跳转获取的页面名称
            menuName: 'well', //页面跳转获取的页面名称
            //查询行的数据
            searchModel: {
                //查询部分默认的绑定值
                wellName: '', //井名
                wellType: '', //井型
                wellCategory: '', //井别
                startDrillDate: '', //开钻日期
                endDrillDate: '', //完钻日期
                currentPage: 1, //当前页
                pageSize: 20 //页大小
            },
            //下拉框字典
            oilfieldOptions: [], //油田
            BlockOptions: [], //区块
            departmentOptions: [], //项目单位
            DrillWellCompanyOptions: [], //钻井公司
            DrillCompanyOptions: [], //钻探公司
            ReservoirTypeOptions: [], //油藏类型
            wellTypeOptions: [], //井型
            wellClassesOptions: [], //井别
            //表格数据
            tableData: [],
            tableHead: [],
            //分页条数据
            pageIdx: 1,
            pageSizes: 20,
            totalData: 0,
            //table展示数据（样式）
            tableHeight: this.$route.query.tableHeight,
            rowStyle: { height: '30px' },
            cellStyle: {
                padding: 0
            },
            //编辑框数据
            editVisible: false,
            form: {}, //每行数据
            idx: -1, //修改行id
            //新增和编辑的判断
            isNewData: false
        };
    },
    created() {
        // console.log("menuName: " + this.$route.query.menuName)
        //开始查询表数据
        this.getTableHead1();
        // this.getTableData1()
        this.FindWellByCondition();
        //获取下拉框数据字典
        this.ListDataDictByCondition();
    },
    mounted() {},
    methods: {
        // 获取表头
        getTableHead1() {
            GetTableHead(this.menuName)
                .then((res) => {
                    this.tableHead = res.data;
                    // console.log(this.tableHead);
                    this.tableHead[10].label = this.tableHead[10].label + '(m)';
                    this.tableHead[11].label = this.tableHead[11].label + '(m)';
                })
                .catch((err) => {
                    console.log('获取表头' + err);
                    this.$message.error('查询出错，请刷新！');
                });
        },
        // 获取表格数据
        getTableData1() {
            GetTableData(this.menuName, this.pageIdx, this.pageSizes, this.searchModel)
                .then((res) => {
                    //console.log(res)
                    if (res.data) {
                        this.tableData = res.data.data;
                        console.log(this.tableData);
                    } else {
                        this.tableData = [];
                    }
                    this.totalData = res.data.total;
                })
                .catch((err) => {
                    console.log('获取表格数据' + err);
                    this.$message.error('查询出错，请刷新！');
                });
        },
        //获取下拉列表
        ListDataDictByCondition() {
            ListDataDictByCondition('油田').then((res) => {
                this.oilfieldOptions = res.data;
            });
            ListDataDictByCondition('区块').then((res) => {
                this.BlockOptions = res.data;
            }),
                ListDataDictByCondition('钻探公司').then((res) => {
                    this.DrillCompanyOptions = res.data;
                }),
                ListDataDictByCondition('钻井公司').then((res) => {
                    this.DrillWellCompanyOptions = res.data;
                }),
                ListDataDictByCondition('油藏类型').then((res) => {
                    this.ReservoirTypeOptions = res.data;
                }),
                ListDataDictByCondition('井型').then((res) => {
                    this.wellClassesOptions = res.data;
                });
            ListDataDictByCondition('井别').then((res) => {
                this.wellTypeOptions = res.data;
            });
            ListDataDictByCondition('项目建设单位') //项目单位
                .then((res) => {
                    this.departmentOptions = res.data;
                });
            // ListDataDictByCondition("采油厂")
            // .then(res => {
            //     this.departmentOptions = res.data
            // });
        },
        //保存新增数据
        InsertOneWell() {
            InsertOneWell(this.form)
                .then((res) => {
                    console.log(res);
                })
                .catch((err) => {
                    console.log('新增数据' + err);
                    this.$message.error('保存出错，请刷新！');
                });
        },
        //保存修改数据
        EditOneWell() {
            EditOneWell(this.form)
                .then((res) => {
                    console.log(res);
                })
                .catch((err) => {
                    console.log('保存数据' + err);
                    this.$message.error('保存出错，请刷新！');
                });
        },
        //删除数据
        DeleteOneWell(wellid) {
            //console.log(this.form.wellid)
            DeleteOneWell(wellid)
                .then((res) => {
                    console.log(res);
                })
                .catch((err) => {
                    console.log('删除数据' + err);
                    this.$message.error('删除出错，请刷新！');
                });
        },
        //查询数据
        FindWellByCondition() {
            FindWellByCondition(this.searchModel)
                .then((res) => {
                    console.log(res); //res.data.wells.records[0]
                    this.tableData = res.data.wells.records;
                    this.totalData = res.data.wells.total;
                })
                .catch((err) => {
                    console.log('查询数据' + err);
                    this.$message.error('查询出错，请刷新！');
                });
        },
        //新增数据
        submitAddModel() {
            this.editVisible = true;
            this.isNewData = true;
            this.form = {};
        },
        //保存新增
        saveNew() {
            //新增的代码
            this.editVisible = false;
            if (this.form.wellname == null || this.form.wellname == '') {
                this.$message.error(`新增数据包名为空，新增失败！`);
            } else {
                this.$message.success(`新增成功`);
                this.InsertOneWell();
                this.FindWellByCondition(); //将展示内容修改
            }
        },
        //点击查询
        submitSearchModel() {
            this.pageIdx = 1;
            this.currentPage = 1;
            this.FindWellByCondition();
            this.$message.success('查询数据成功！');
        },
        // 点击具体行数据--编辑
        handleEdit(index, row) {
            this.idx = index;
            this.editVisible = true;
            this.isNewData = false;
            this.form = row;
        },
        // 保存编辑数据
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form); //将展示内容修改
            //修改数据库数据操作
            this.EditOneWell();
        },
        //点击具体行数据--删除
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                // confirmButtonText: '确定',
                // cancelButtonText: '取消',
                type: 'warning'
            })
            .then(() => {
                this.$message.success('删除成功');
                this.tableData.splice(index, 1);
                //删除数据库数据操作
                this.DeleteOneWell(row.wellid);
            })
            .catch(() => {});
        },
        //点击具体行导入数据--信息录入
        handleImport(index, row) {
            //进行路径的跳转
            localStorage.setItem('wellid', row.wellid);
            localStorage.setItem('wellname', row.wellname);
            localStorage.setItem('tableHeight', this.tableHeight);
            let path = '/BaseWellData';
            this.$router.push({
                path: path,
                query: {
                    // wellid: row.wellid,
                    // wellname: row.wellname,
                }
            });
        },
        // 分页导航
        handlePageChange(page) {
            this.pageIdx = page;
            this.searchModel.currentPage = page;
            this.FindWellByCondition();
        },
        //excel导入数据
        ImportWell(item) {
            const loading = this.$loading({
                lock: true,
                text: '数据导入中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            const formData = new FormData();
            formData.append('file', item.file);
            importExcel(formData, 'Well')
                .then((res) => {
                    console.log('excel导入...');
                    console.log(res);
                    this.FindWellByCondition();
                    loading.close()
                })
                .catch((err) => {
                    console.log(err);
                    loading.close()
                });
        },
        //下载模板
        DownLoadModel() {
            //下载excel模板
            downTemplateExcel('Well').then((res) => {
                console.log(res);
                if (!res) {
                    return;
                }
                const link = document.createElement('a');
                let blob = new Blob([res]);
                link.style.display = 'none';
                link.href = URL.createObjectURL(blob);
                link.setAttribute('download', '井眼信息模板(Well).xlsx');
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            });
        }
    },
    computed: {
        height() {
            if (document.body.clientWidth > 800 && document.body.clientWidth < 2000) {
                return '1000px';
            } else {
                return '100%';
            }
        }
    }
};
</script>

<style scoped>
.card >>> .el-card__body {
    padding: 5px;
}
.card >>> .el-card__header {
    font-size: 17px;
    padding: 5px 10px;
    /* padding: 5px 10px !important; */
    height: 40px;
    line-height: 30px;
    background-color: #eef0f6;
}
.upload-demo >>> .el-upload--text {
    width: 57px;
    height: 32px;
    border-radius: 0px;
    border: none;
    display: flex;
    align-items: center;
}
/* .upload-demo {
    width: 20px;
    height: 38px;
} */
</style>